local jiuzhu = fk.CreateSkill {
  name = "kl__jiuzhu"
}


jiuzhu:addEffect(fk.GameStart, {
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(jiuzhu.name)
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local to = room:askToChoosePlayers(
      player,
      {
        targets = room:getOtherPlayers(player),
        min_num = 1,
        max_num = 1,
        prompt = "#kl__jiuzhu-mark",
        skill_name = jiuzhu.name,
      }
    )
    if #to > 0 then
      event:setCostData(self, { tos = to })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    room:setPlayerMark(to, "@kl__jiuzhu", 1)
  end,
})

jiuzhu:addEffect(fk.Damaged, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(jiuzhu.name) and player:usedEffectTimes(self.name) < 1 and not player:isNude() and data.from ~= nil
    and (data.to:getMark("@kl__jiuzhu") > 0 or data.to == player)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local to_discard = room:askToDiscard(player, {
      min_num = 1,
      max_num = 1,
      cancelable = true,
      include_equip = true,
      prompt = "#kl__jiuzhu-discard",
      skill_name = jiuzhu.name,
      skip = true,
    })
    if #to_discard > 0 then
      event:setCostData(self, { card = to_discard[1] })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:throwCard(event:getCostData(self).card,jiuzhu.name, player, player)
    local choice = room:askToChoice(player, {
      choices = { "kl__jiuzhu-slash", "kl__jiuzhu-duel"},
      skill_name = jiuzhu.name,
      cancelable = false,
      prompt = "请选择视为对伤害来源使用的牌",
    })
    if choice == "kl__jiuzhu-slash" then
      local use = {
        from = player,
        tos = {data.from},
        card = Fk:cloneCard("slash"),
        bypass_times = true,
        bypass_distances = true,
        extraUse = true,
      }
      use.card.skillName = jiuzhu.name
      if use then
        use.disresponsiveList = room.players
        room:useCard(use)
      end
    elseif choice == "kl__jiuzhu-duel" then
      local use = {
        from = player,
        tos = {data.from},
        card = Fk:cloneCard("duel"),
        extraUse = true,
      }
      use.card.skillName = jiuzhu.name
      if use then
        use.additionalDamage = (use.additionalDamage or 0) + 1
        room:useCard(use)
      end
    end
  end,
})

jiuzhu:addEffect(fk.TargetSpecified, {
  can_refresh = function(self, event, target, player, data)
    return table.contains(data.card.skillNames, jiuzhu.name) and not data.to.dead and data.card.trueName == "slash"
  end,
  on_refresh = function(self, event, target, player, data)
    data.to:addQinggangTag(data)
  end,
})


return jiuzhu
